package cn.com.basic.framework.web.base.controller;

import cn.com.basic.framework.web.base.request.BaseQueryPage;
import cn.com.basic.framework.utils.RequestUtil;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * BaseController
 * @Author Cuixk
 * 2020/1/9 09:37
 */
public class BaseController<T> {


    /**
     * 基础分页方法
     * @return
     */
    protected BaseQueryPage getBaseQueryPage() {
        BaseQueryPage BaseQueryPage = new BaseQueryPage();
        Integer page = RequestUtil.getIntegerParameter("page");
        if (page != null && page > 0) {
            BaseQueryPage.setPage(page);
        }
        Integer pageSize = RequestUtil.getIntegerParameter("pageSize");
        if (pageSize != null && pageSize > 0) {
            BaseQueryPage.setPageSize(pageSize);
        }
        Map<String, Object> condition = new HashMap<>(16);
        Map<String, String> searchCondition = new HashMap<>(16);
        Map<String, Object> rangeCondition = new HashMap<>(16);
        Map<String, Boolean> orderByCondition = new LinkedHashMap<>();
        Map<String, String[]> params = RequestUtil.getRequest().getParameterMap();
        for (String key : params.keySet()) {
            String value = params.get(key)[0];
            if (value.length() == 0) {
                continue;
            }
            if (key.startsWith("p_")) {
                condition.put(key.substring(2), value);
            } else if (key.startsWith("s_")) {
                searchCondition.put(key.substring(2), value);
            } else if (key.startsWith("o_")) {
                orderByCondition.put(key.substring(2), "T".equalsIgnoreCase(value));
            } else if (key.length() > 3 && key.charAt(2) == '_') {
                rangeCondition.put(key, value);
            }
        }
        BaseQueryPage.setCondition(condition);
        BaseQueryPage.setSearchCondition(searchCondition);
        BaseQueryPage.setRangeCondition(rangeCondition);
        BaseQueryPage.setOrderByCondition(orderByCondition);
        return BaseQueryPage;
    }
}
